/**
 *异常过滤器
 * **/
import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
  HttpStatus,
  Logger,
} from '@nestjs/common';
import { ApiResponse } from '@common/utils/api-response.util';
import { BusinessCode } from '@common/enums/business-code.enum';
import type { AppResponse } from '@common/types/public.http';

@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
  private readonly logger = new Logger(HttpExceptionFilter.name);

  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
    const message = exception.message || 'Internal Server Error';

    // 记录日志
    this.logger.error(`Error [${status}]: ${message}`, exception.stack);

    response.status(status).json({
      code: status,
      message: message,
      timestamp: new Date().toISOString(),
    });
  }
}
